package com.ksc.storekeeper.server.web.controllers;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import com.ksc.storekeeper.server.utils.AuthenticationUtil;

@Controller
public class MainController {

	@Autowired
	ServletContext servletContext;

	public final static int INDEX_FILE_EXTENSION = 1;

	@RequestMapping(method = RequestMethod.GET, value = "/main")
	public String getHome(ModelMap modelMap) {
		modelMap.put("username", AuthenticationUtil.getCurrentUser().getUserName());
		return "main";
	}

	@RequestMapping(method = RequestMethod.POST, value = "/upload")
	public ModelAndView uploadImage(@RequestParam("code") String code, @RequestParam("uploadImage") CommonsMultipartFile image, HttpServletRequest request, ModelMap modelMap) {
		String extension;
		try {

			if (image.getSize() > 1000000) {
				return new ModelAndView(new FormSubmitView("error", "File Size Exceeded 10B"));
			}
			InputStream inputStream = image.getInputStream();
			String fileName = image.getOriginalFilename();
			extension = fileName.substring(fileName.indexOf('.'));

			String locationToUpload = servletContext.getRealPath("") + "/images/product/" + code + extension;
			System.out.println(locationToUpload);
			OutputStream outputStream = new FileOutputStream(locationToUpload);
			int readBytes = 0;
			byte[] buffer = new byte[10000];
			while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
				outputStream.write(buffer, 0, readBytes);
			}
			outputStream.close();
			inputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView(new FormSubmitView("error", "Internal Server Error!!!"));
		}
		return new ModelAndView(new FormSubmitView("success", code + extension));
	}

	class FormSubmitView implements View {

		String elementId;

		String text;

		public FormSubmitView(String elementId, String text) {
			this.elementId = elementId;
			this.text = text;
		}

		@Override
		public void render(Map<String, ?> arg0, HttpServletRequest arg1, HttpServletResponse response) throws Exception {
			response.getWriter().write("<html><span id='" + elementId + "'>" + text + "</span></html>");
		}

		@Override
		public String getContentType() {
			return "text/plain";
		}
	}

}
